package com.whfc.ms.api.emp.service.impl;

import com.whfc.common.exception.BizException;
import com.whfc.common.file.FileHandler;
import com.whfc.common.file.FilePathConfig;
import com.whfc.common.result.ListData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.ImageUtil;
import com.whfc.common.util.RandomUtil;
import com.whfc.emp.dto.AppEmpContractDTO;
import com.whfc.emp.param.AppContractAddParam;
import com.whfc.emp.param.AppContractEditParam;
import com.whfc.emp.service.AppContractService;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.ms.api.emp.service.MsAppContractService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @ClasssName MsAppContractServiceImpl
 * @Description 人员合同
 * @Author hw
 * @Date 2020/12/30 16:23
 * @Version 1.0
 */
@Service
public class MsAppContractServiceImpl implements MsAppContractService {

    @DubboReference(interfaceClass = AppContractService.class, version = "1.0.0")
    private AppContractService appContractService;

    @Autowired
    private FilePathConfig filePathConfig;

    @Autowired
    private FileHandler fileHandler;

    @Override
    public ListData<AppEmpContractDTO> list(Integer empId) {
        List<AppEmpContractDTO> list = appContractService.list(empId);
        return new ListData<>(list);
    }

    @Override
    public void add(AppContractAddParam param) {
        appContractService.add(param);
    }

    @Override
    public void edit(AppContractEditParam param) {
        appContractService.edit(param);
    }

    @Override
    public void del(Integer id) {
        appContractService.del(id);
    }

    @Override
    public OssPathDTO upload(String base64Str, MultipartFile file) throws IOException {
        OssPathDTO ossPathDTO = new OssPathDTO();
        
        if (file != null) {
            // 获取原始文件名和文件后缀
            String originalFilename = file.getOriginalFilename();
            String fileSuffix = "png"; // 默认后缀
            
            if (StringUtils.isNotBlank(originalFilename)) {
                int lastDotIndex = originalFilename.lastIndexOf(".");
                if (lastDotIndex > 0) {
                    fileSuffix = originalFilename.substring(lastDotIndex + 1);
                }
            }
            
            String fileName = filePathConfig.getFilePath("emp/contract/file", RandomUtil.getRandomFileName(), fileSuffix);
            InputStream inputStream = file.getInputStream();
            String upload = fileHandler.upload(fileName, inputStream);
            ossPathDTO.setPath(upload);
            return ossPathDTO;
        }
    
        if (!StringUtils.isBlank(base64Str)) {
            // base64上传保持原有逻辑，使用png格式
            String fileName = filePathConfig.getFilePath("emp/contract/file", RandomUtil.getRandomFileName(), "png");
            byte[] imageByte = ImageUtil.base64ToImage(base64Str);
            ByteArrayInputStream inputStream = new ByteArrayInputStream(imageByte);
            String upload = fileHandler.upload(fileName, inputStream);
            ossPathDTO.setPath(upload);
            return ossPathDTO;
        }

        throw new BizException(ResultEnum.FAILURE.getCode(), "参数错误");
    }
}